package com.sellerSystem.util;

import java.net.InetAddress;
import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;

public class Deblocking implements InitializingBean {

	public void afterPropertiesSet() {
		/*System.out.println("............========================");
		ConnectionFactory factory = new ActiveMQConnectionFactory(
				"tcp://127.0.0.1:61616");
		Destination queue1 = new ActiveMQQueue("seller");
		Connection con;
		try {
			con = factory.createConnection();

			con.start();
			// System.out.println(con);
			Session sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageConsumer consumer = sen.createConsumer(queue1);
			consumer.setMessageListener(new MessageListener() {

				@Override
				public void onMessage(Message message) {
					TextMessage msg = (TextMessage) message;
					JSONObject json = null;
					String recMessage = null;
					Integer sellerId = 0;
					Integer sellerState = 0;
					try {
						json = new JSONObject(msg.getText());
						recMessage = json.get("query").toString();

						System.out.println(recMessage);

						if (recMessage.equals("all")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url2 = ("http://" + ip + ":8080/sellerSystem/findseller");
							System.out.println(url2);
							JSONObject jso2 = new JSONObject();
							jso2.put("url", url2);

							TextMessage msg2 = sen1.createTextMessage(jso2
									.toString());

							producer.send(msg2);
							System.out.println(msg2);

							producer.close();
							sen1.close();
						} else if (recMessage.equals("modifystatebyid")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							sellerState = json.getInt("sellerState");
							System.out.println(".................."
									+ sellerState);
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url6 = ("http://"
									+ ip
									+ ":8080/sellerSystem/modifystate?sellerId=" + sellerId);
							System.out.println(url6);
							JSONObject jso6 = new JSONObject();
							jso6.put("url", url6);
							System.out.println(jso6.getString("url")
									+ "@@@@@@@@@@@@@@@@@@@@@@@");
							TextMessage msg6 = sen1.createTextMessage(jso6
									.toString());

							producer.send(msg6);
							System.out.println(msg6);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("querybyid")) {
							Session sen1 = con.createSession(true,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							System.out
									.println("querybyid............................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url3 = ("http://"
									+ ip
									+ ":8080/sellerSystem/findsellerbyid?sellerId=" + sellerId);
							System.out.println(url3);
							JSONObject jso3 = new JSONObject();
							jso3.put("url", url3);

							TextMessage msg3 = sen1.createTextMessage(jso3
									.toString());

							producer.send(msg3);
							System.out.println(msg3);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("allbill")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							System.out
									.println("allbill.......................................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url4 = ("http://" + ip + ":8080/sellerSystem/allbill");
							System.out.println(url4);
							JSONObject jso4 = new JSONObject();
							jso4.put("url", url4);

							TextMessage msg4 = sen1.createTextMessage(jso4
									.toString());

							producer.send(msg4);
							System.out.println(msg4);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("billbyid")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url5 = ("http://" + ip
									+ ":8080/sellerSystem/billbyid?sellerId=" + sellerId);
							System.out.println(url5);
							JSONObject jso5 = new JSONObject();
							jso5.put("url", url5);

							TextMessage msg5 = sen1.createTextMessage(jso5
									.toString());

							producer.send(msg5);
							System.out.println(msg5);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("joinblacklist")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							System.out
									.println("joinblacklist.................................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url7 = ("http://"
									+ ip
									+ ":8080/sellerSystem/joinblacklist?sellerId=" + sellerId);
							System.out.println(url7);
							JSONObject jso7 = new JSONObject();
							jso7.put("url", url7);

							TextMessage msg7 = sen1.createTextMessage(jso7
									.toString());

							producer.send(msg7);
							System.out.println(msg7);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("ifblacklist")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							System.out
									.println("ifBlacklist.................................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url8 = ("http://" + ip + ":8080/sellerSystem/ifblacklist");
							System.out.println(url8);
							JSONObject jso8 = new JSONObject();
							jso8.put("url", url8);

							TextMessage msg8 = sen1.createTextMessage(jso8
									.toString());

							producer.send(msg8);
							System.out.println(msg8);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("noblack")) {
							Session sen1 = con.createSession(true,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							System.out
									.println("noblack............................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url9 = ("http://" + ip
									+ ":8080/sellerSystem/noblack?sellerId=" + sellerId);
							System.out.println(url9);
							JSONObject jso9 = new JSONObject();
							jso9.put("url", url9);

							TextMessage msg9 = sen1.createTextMessage(jso9
									.toString());

							producer.send(msg9);
							System.out.println(msg9);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("approvalfail")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							sellerId = json.getInt("sellerId");
							sellerState = json.getInt("sellerState");
							System.out.println(".................."
									+ sellerState);
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url6 = ("http://"
									+ ip
									+ ":8080/sellerSystem/approvalfail?sellerId=" + sellerId);
							System.out.println(url6);
							JSONObject jso6 = new JSONObject();
							jso6.put("url", url6);
							System.out.println(jso6.getString("url")
									+ "@@@@@@@@@@@@@@@@@@@@@@@");
							TextMessage msg6 = sen1.createTextMessage(jso6
									.toString());

							producer.send(msg6);
							System.out.println(msg6);
							producer.close();
							sen1.close();
						} else if (recMessage.equals("complaintinfo")) {
							Session sen1 = con.createSession(false,
									Session.AUTO_ACKNOWLEDGE);
							System.out
									.println("complaintinfo.................................");
							Destination queue = new ActiveMQQueue("backstage");
							MessageProducer producer = sen1
									.createProducer(queue);
							String ip = InetAddress.getLocalHost()
									.getHostAddress();
							String url8 = ("http://" + ip + ":8080/sellerSystem/complaintinfo");
							System.out.println(url8);
							JSONObject jso8 = new JSONObject();
							jso8.put("url", url8);

							TextMessage msg8 = sen1.createTextMessage(jso8
									.toString());

							producer.send(msg8);
							System.out.println(msg8);
							producer.close();
							sen1.close();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
					}
				}
			});

			new Scanner(System.in).nextLine();
			consumer.close();
			sen.close();
			con.close();
		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
	}

}